package org.dr.springmvcdemo.scheduleTask.Quartz.jobListen;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

public class MyJobListener implements JobListener {
    private static final ThreadLocal<Integer> callCount = new ThreadLocal<>();

    @Override
    public String getName() {
        int count = callCount.get() != null ? callCount.get() : 0;
        count++;
        callCount.set(count);
        String name = getClass().getSimpleName();
        System.out.println("监听器的名称是：" + name + " (调用次数: " + count + ")");
        return name;
    }

    @Override
    public void jobToBeExecuted(JobExecutionContext context) {
        String jobName = context.getJobDetail().getKey().getName();
        System.out.println("Job的名称是：" + jobName + "\tScheduler在JobDetail将要被执行时调用这个方法");
    }

    @Override
    public void jobExecutionVetoed(JobExecutionContext context) {
        String jobName = context.getJobDetail().getKey().getName();
        System.out.println("Job的名称是：" + jobName + "\tScheduler在JobDetail即将被执行，但又被TriggerListerner否决时会调用该方法");
    }

    @Override
    public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
        String jobName = context.getJobDetail().getKey().getName();
        System.out.println("Job的名称是：" + jobName + "\tScheduler在JobDetail被执行之后调用这个方法");
    }
}
